
(in-package #:smugmug)

(defmethod get-categories ((session session) &optional nickname site-password)
  (let ((parameters '()))
    (when nickname (push `("NickName" . ,nickname) parameters))
    (when site-password (push `("SitePassword" . ,site-password) parameters))
    (let ((response (rest-request :session session
                                  :method "smugmug.categories.get"
                                  :parameters parameters)))
      (let ((categories '()))
        (dom:map-node-list #'(lambda (category-node)
                               (push (make-instance 'category 
                                                    :category-id (dom:get-attribute category-node "id")
                                                    :title (dom:get-attribute category-node "Title")
                                                    :session session)
                                     categories))
                           (dom:get-elements-by-tag-name response "Category"))
        (reverse categories)))))

(defmethod create-category ((session session) title)
  (let ((response (rest-request :session session
                                :method "smugmug.categories.create"
                                :parameters `(("Name" . ,title)))))
    (make-instance 'category
                   :category-id (dom:get-attribute (elt (dom:get-elements-by-tag-name response "Category") 0) "id")
                   :title title
                   :session session)))

(defmethod delete-category ((category category))
  (rest-request :session (session category)
                :method "smugmug.categories.delete"
                :parameters `(("CategoryID" . ,(category-id category)))))

(defmethod rename-category ((category category) new-title)
  (rest-request :session (session category)
                :method "smugmug.categories.rename"
                :parameters `(("CategoryID" . ,(category-id category))
                              ("Name" . ,new-title))))

(defmethod print-object ((category category) stream)
  (print-unreadable-object (category stream :type t :identity :t)
    (format stream "~A ~S"
            (category-id category)
            (title category))))
